package com.spring.sunflower.dialog

import android.app.Activity
import android.content.Context
import android.graphics.Rect
import android.view.View
import com.lxj.xpopup.core.BottomPopupView
import android.widget.EditText
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.widget.ImageView
import com.blankj.utilcode.util.ToastUtils
import com.spring.sunflower.R
import com.spring.sunflower.util.LogUtils


/**
 * Description: 自定义带有输入框的Bottom弹窗
 * Create by dance, at 2019/2/27
 */
class BottomCommentInputDialog(var context: Activity) : BottomPopupView(context) {
    private var keyboardShow = false
    private var clickRelease = false
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_bottom_comment_input
    }

    override fun onCreate() {
        super.onCreate()
        val editText = findViewById<EditText>(R.id.et_comment)
        editText.viewTreeObserver.addOnGlobalLayoutListener {
            val rect = Rect()
            val decorView: View = context.window.decorView
            decorView.getWindowVisibleDisplayFrame(rect)
            val screenHeight: Int = decorView.getRootView().getHeight()
            val diffHeight = screenHeight - rect.bottom
            val visible = diffHeight > screenHeight / 3
            LogUtils.logD("BottomCommentInputDialog", "visible=$visible")
            if (visible) {
                keyboardShow = visible
            }
            if (keyboardShow && !visible) {
                keyboardShow = false
                dismiss()
            }
//            val rect = Rect()
            //                View decorView = getContext().getW.getDecorView();
//                decorView.getWindowVisibleDisplayFrame(rect);
//                int screenHeight = decorView.getRootView().getHeight();
//                int diffHeight = screenHeight - rect.bottom;
//                boolean visible = diffHeight > screenHeight / 3;
//                printD("rectBottom=" + rect.bottom + "---screenHeight=" + screenHeight);
//                printD("v=" + visible);
//                if (visible) {
//                    scrollToEnd();
//                    keyboardShow = true;
//                } else {
//                    printD("xxx");
//                }
        }
    }

    override fun onShow() {
        super.onShow()
        findViewById<ImageView>(R.id.ivRelease).setOnClickListener {
            val et = findViewById<EditText>(R.id.et_comment)
            if (et.text.toString().trim().isNullOrEmpty()) {
                ToastUtils.showShort("评论内容不能为空")
                return@setOnClickListener
            }
            clickRelease = true
            listener?.onReleaseClick()
            dismiss()
        }

    }

    override fun onDismiss() {
        super.onDismiss()
        //        Log.e("tag", "CustomEditTextBottomPopup  onDismiss");
    }

    //    @Override
    val comment: String
        get() {
            val et = findViewById<EditText>(R.id.et_comment)
            return et.text.toString()
        }
    var listener: OnReleaseClickListener? = null
    fun setOnReleaseClickListener(l: OnReleaseClickListener) {
        listener = l
    }

    val isClickRelease: Boolean
        get() {
            return clickRelease
        }

    interface OnReleaseClickListener {
        fun onReleaseClick()
    }
}